找传奇、传世资源到传世资源站!

c# 经典TreeListView方法 实例源码下载

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

c# 经典TreeListView方法 实例源码下载 常用C#方法-第1张using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace TreeListViewDemo{ public partial class Form1 : Form { private void AddTreeViewNodes() { // 添加根节点 TreeNode tn = new TreeNode(); tn.Text = "金庸小说"; tn.ImageIndex = 0; tn.SelectedImageIndex = 0; tv.Nodes.Add((TreeNode)(tn.Clone())); // 添加4个子节点 tn.Text = "射雕英雄传"; tn.ImageIndex = 1; tn.SelectedImageIndex = 2; tv.Nodes[0].Nodes.Add((TreeNode)(tn.Clone())); tn.Text = "神雕侠侣"; tn.ImageIndex = 1; tn.SelectedImageIndex = 2; tv.Nodes[0].Nodes.Add((TreeNode)(tn.Clone())); tn.Text = "天龙八部"; tn.ImageIndex = 1; tn.SelectedImageIndex = 2; tv.Nodes[0].Nodes.Add((TreeNode)(tn.Clone())); tn.Text = "倚天屠龙记"; tn.ImageIndex = 1; tn.SelectedImageIndex = 2; tv.Nodes[0].Nodes.Add((TreeNode)(tn.Clone())); // 添加根节点 tn.Text = "古龙小说"; tn.ImageIndex = 0; tn.SelectedImageIndex = 0; tv.Nodes.Add((TreeNode)(tn.Clone())); // 添加根节点 tn.Text = "黄易小说"; tn.ImageIndex = 0; tn.SelectedImageIndex = 0; tv.Nodes.Add((TreeNode)(tn.Clone())); // 添加3个子节点 tn.Text = "大唐双龙传"; tn.ImageIndex = 1; tn.SelectedImageIndex = 2; tv.Nodes[2].Nodes.Add((TreeNode)(tn.Clone())); tn.Text = "寻秦记"; tn.ImageIndex = 1; tn.SelectedImageIndex = 2; tv.Nodes[2].Nodes.Add((TreeNode)(tn.Clone())); tn.Text = "第1章 时空机器"; tn.ImageIndex = 3; tn.SelectedImageIndex = 4; tv.Nodes[2].Nodes[1].Nodes.Add((TreeNode)(tn.Clone())); tn.Text = "第2章 墨子剑法"; tn.ImageIndex = 3; tn.SelectedImageIndex = 4; tv.Nodes[2].Nodes[1].Nodes.Add((TreeNode)(tn.Clone())); tn.Text = "第3章 初会秦皇"; tn.ImageIndex = 3; tn.SelectedImageIndex = 4; tv.Nodes[2].Nodes[1].Nodes.Add((TreeNode)(tn.Clone())); tn.Text = "第4章 一统天下"; tn.ImageIndex = 3; tn.SelectedImageIndex = 4; tv.Nodes[2].Nodes[1].Nodes.Add((TreeNode)(tn.Clone())); tn.Text = "翻云覆雨"; tn.ImageIndex = 1; tn.SelectedImageIndex = 2; tv.Nodes[2].Nodes.Add((TreeNode)(tn.Clone())); } public Form1() { InitializeComponent(); // 调用向树视图中添加节点的方法 AddTreeViewNodes(); } private void contextMenuOfTv_Opening(object sender, CancelEventArgs e) { } private void tv_AfterSelect(object sender, TreeViewEventArgs e) { // 在文本框中显示当前选择节点的完全路径 txtPath.Text = tv.SelectedNode.FullPath; // 在列表视图中显示当前选择节点的所有子节点 lv.Clear();// 首先清除列表视图的所有列和项 // 向列表视图中添加列,当前选择节点不同,列的标题 // 也不同 if (tv.SelectedNode.ImageIndex == 0) { lv.Columns.Add("书名", 160, HorizontalAlignment.Left); lv.Columns.Add("页数", 60, HorizontalAlignment.Right); lv.Columns.Add("价格", 60, HorizontalAlignment.Right); } else { lv.Columns.Add("标题", 160, HorizontalAlignment.Left); lv.Columns.Add("字数", 60, HorizontalAlignment.Right); lv.Columns.Add("页数", 60, HorizontalAlignment.Right); } int i = 300, j = 20; int imgIndex = 0; if (tv.SelectedNode.ImageIndex == 1) imgIndex = 1; // 向列表视图中添加项 foreach (TreeNode tn in tv.SelectedNode.Nodes) { ListViewItem lvi = new ListViewItem(tn.Text, imgIndex); lvi.SubItems.Add(i.ToString()); lvi.SubItems.Add(j.ToString()); lv.Items.Add(lvi); i = 100; j = 10; } } private void lv_SelectedIndexChanged(object sender, EventArgs e) { // 把当前项的标题附加到树视图当前选择节点的完全路径的 // 后面并显示在文本框中 if (lv.SelectedItems.Count > 0) txtPath.Text = tv.SelectedNode.FullPath tv.PathSeparator lv.SelectedItems[0].Text; } private void miLargeIcon_Click(object sender, EventArgs e) { miLargeIcon.Checked = true; miSmallIcon.Checked = false; miList.Checked = false; miDetail.Checked = false; lv.View = System.Windows.Forms.View.LargeIcon; } private void miSmallIcon_Click(object sender, EventArgs e) { miLargeIcon.Checked = false; miSmallIcon.Checked = true; miList.Checked = false; miDetail.Checked = false; lv.View = System.Windows.Forms.View.SmallIcon; } private void miList_Click(object sender, EventArgs e) { miLargeIcon.Checked = false; miSmallIcon.Checked = false; miList.Checked = true; miDetail.Checked = false; lv.View = System.Windows.Forms.View.List; } private void miDetail_Click(object sender, EventArgs e) { miLargeIcon.Checked = false; miSmallIcon.Checked = false; miList.Checked = false; miDetail.Checked = true; lv.View = System.Windows.Forms.View.Details; } private void miNewNode_Click(object sender, EventArgs e) { TreeNode tn = new TreeNode("新建节点"); if (tv.SelectedNode.ImageIndex == 0) { tn.ImageIndex = 1; tn.SelectedImageIndex = 2; } else { tn.ImageIndex = 3; tn.SelectedImageIndex = 4; } tv.SelectedNode.Nodes.Add(tn); } private void miDeleteNode_Click(object sender, EventArgs e) { tv.SelectedNode.Remove(); } private void miExpandAll_Click(object sender, EventArgs e) { tv.ExpandAll(); } private void miCollapseAll_Click(object sender, EventArgs e) { tv.CollapseAll(); } private void miChangeSep_Click(object sender, EventArgs e) { if (tv.PathSeparator == "\\") tv.PathSeparator = "|"; else tv.PathSeparator = "\\"; } private void miShowPlusMinus_Click(object sender, EventArgs e) { miShowPlusMinus.Checked =! miShowPlusMinus.Checked; tv.ShowPlusMinus = miShowPlusMinus.Checked; } private void miShowLine_Click(object sender, EventArgs e) { miShowLine.Checked =! miShowLine.Checked ; tv.ShowLines = miShowLine.Checked; } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复